package com.tetris.rule;

import com.tetris.block.Block;

/**
 * 经典俄罗斯方块规则实现
 */
public class ClassicTetrisRules implements GameRules {
    @Override
    public boolean isValidMove(int[][] grid, Block block, Direction direction) {
        // 实现方块移动验证逻辑
        int[][] shape = block.getShape();
        int newX = block.getX();
        int newY = block.getY();
        
        switch (direction) {
            case LEFT:
                newX--;
                break;
            case RIGHT:
                newX++;
                break;
            case DOWN:
                newY++;
                break;
            default:
                return false; // 向上移动不处理
        }
        
        // 检查新位置是否有效
        for (int i = 0; i < shape.length; i++) {
            for (int j = 0; j < shape[i].length; j++) {
                if (shape[i][j] != 0) {
                    int x = newX + j;
                    int y = newY + i;
                    
                    // 检查边界
                    if (x < 0 || x >= grid[0].length || y >= grid.length) {
                        return false;
                    }
                    
                    // 检查是否与已有方块重叠
                    if (y >= 0 && grid[y][x] != 0) {
                        return false;
                    }
                }
            }
        }
        
        return true;
    }
    
    @Override
    public boolean isValidRotation(int[][] grid, Block block, Rotation rotation) {
        // 实现方块旋转验证逻辑
        int[][] rotatedShape = block.getRotatedShape(rotation);
        int x = block.getX();
        int y = block.getY();
        
        // 检查旋转后的位置是否有效
        for (int i = 0; i < rotatedShape.length; i++) {
            for (int j = 0; j < rotatedShape[i].length; j++) {
                if (rotatedShape[i][j] != 0) {
                    int gridX = x + j;
                    int gridY = y + i;
                    
                    // 检查边界
                    if (gridX < 0 || gridX >= grid[0].length || gridY >= grid.length) {
                        return false;
                    }
                    
                    // 检查是否与已有方块重叠
                    if (gridY >= 0 && grid[gridY][gridX] != 0) {
                        return false;
                    }
                }
            }
        }
        
        return true;
    }
    
    @Override
    public int clearLines(int[][] grid) {
        // 实现消除满行逻辑
        int linesCleared = 0;
        int rows = grid.length;
        int cols = grid[0].length;
        
        for (int i = rows - 1; i >= 0; i--) {
            boolean isLineFull = true;
            
            for (int j = 0; j < cols; j++) {
                if (grid[i][j] == 0) {
                    isLineFull = false;
                    break;
                }
            }
            
            if (isLineFull) {
                linesCleared++;
                
                // 将上面的行下移
                for (int k = i; k > 0; k--) {
                    System.arraycopy(grid[k-1], 0, grid[k], 0, cols);
                }
                
                // 清空顶行
                for (int j = 0; j < cols; j++) {
                    grid[0][j] = 0;
                }
                
                // 重新检查当前行（现在是上一行移下来的）
                i++;
            }
        }
        
        return linesCleared;
    }
    
    @Override
    public int calculateScore(int linesCleared) {
        // 根据消除的行数计算分数
        switch (linesCleared) {
            case 1: return 100;
            case 2: return 300;
            case 3: return 500;
            case 4: return 800;
            default: return 0;
        }
    }
    
    @Override
    public boolean isGameOver(int[][] grid) {
        // 检查游戏是否结束（顶部有方块）
        for (int j = 0; j < grid[0].length; j++) {
            if (grid[0][j] != 0) {
                return true;
            }
        }
        return false;
    }
}
